﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;

namespace He.Web.manage.nav
{
    public partial class edit : He.Web.UI.Manage
    {
        private string action = Enums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        public string imgurl = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = GetString("action");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                    return;
                }
                if (!new He.BLL.nav().Exists(this.id))
                {
                    JscriptMsg(350, 230, "错误提示", "信息不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                if (action == Enums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.nav bll = new BLL.nav();
            Model.nav model = bll.GetModel(_id);
            txtTitle.Text = model.title;
            txtImgUrl.Text = model.img_url;
            txtLinkUrl.Text = model.link_url;
            rblTarget.SelectedValue = model.target;
            txtSortId.Text = model.sort_id.ToString();
            chkLock.Checked = model.is_lock == 1 ? true : false;
            imgurl = model.img_url;

            //所有栏目绑定
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.nav model = new Model.nav();
            BLL.nav bll = new BLL.nav();
            model.title = txtTitle.Text.Trim();
            model.img_url = txtImgUrl.Text.Trim();
            model.link_url = txtLinkUrl.Text.Trim();
            model.target = rblTarget.SelectedValue;
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.is_lock = chkLock.Checked ? 1 : 0;
            model.lang = lang;

            //保存
            if (bll.Add(model) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            BLL.nav bll = new BLL.nav();
            Model.nav model = bll.GetModel(_id);
            model.title = txtTitle.Text.Trim();
            model.img_url = txtImgUrl.Text.Trim();
            model.link_url = txtLinkUrl.Text.Trim();
            model.target = rblTarget.SelectedValue;
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.is_lock = chkLock.Checked ? 1 : 0;

            if (!bll.Update(model))
            {
                return false;
            }
            return true;
        }
        #endregion


        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    JscriptPrint("保存过程中发生错误啦！", "list.aspx?lang=" + lang, "Error");
                    return;
                }
                JscriptPrint("修改信息成功啦！", "list.aspx?lang=" + lang, "Success");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptPrint("保存过程中发生错误啦！", "list.aspx?lang=" + lang, "Error");
                    return;
                }
                JscriptPrint("添加信息成功啦！", "list.aspx?lang=" + lang, "Success");
            }
        }
    }
}